package com.example.dw.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.dw.entity.QuestionBank;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 题库Mapper接口
 * @author system
 * @since 2024-01-01
 */
@Mapper
public interface QuestionBankMapper extends BaseMapper<QuestionBank> {

    /**
     * 根据题目ID获取题目详情
     */
    @Select("SELECT * FROM questionbank WHERE question_id = #{questionId}")
    QuestionBank getQuestionById(@Param("questionId") Integer questionId);

    /**
     * 根据课程类型ID随机获取题目
     */
    @Select("SELECT * FROM questionbank WHERE courseTypeid = #{courseTypeid} " +
            "ORDER BY RAND() LIMIT #{limit}")
    List<QuestionBank> getRandomQuestionsByCourseType(@Param("courseTypeid") Integer courseTypeid,
                                                      @Param("limit") Integer limit);

    /**
     * 根据课程类型ID和难度获取题目
     */
    @Select("SELECT * FROM questionbank WHERE courseTypeid = #{courseTypeid} " +
            "AND difficulty = #{difficulty} ORDER BY RAND() LIMIT #{limit}")
    List<QuestionBank> getQuestionsByCourseTypeAndDifficulty(@Param("courseTypeid") Integer courseTypeid,
                                                             @Param("difficulty") String difficulty,
                                                             @Param("limit") Integer limit);

    /**
     * 根据课程类型ID获取题目总数
     */
    @Select("SELECT COUNT(*) FROM questionbank WHERE courseTypeid = #{courseTypeid}")
    Integer getQuestionCountByCourseType(@Param("courseTypeid") Integer courseTypeid);

    /**
     * 根据课程类型ID和难度获取题目总数
     */
    @Select("SELECT COUNT(*) FROM questionbank WHERE courseTypeid = #{courseTypeid} " +
            "AND difficulty = #{difficulty}")
    Integer getQuestionCountByCourseTypeAndDifficulty(@Param("courseTypeid") Integer courseTypeid,
                                                      @Param("difficulty") String difficulty);

    /**
     * 根据题目ID列表批量获取题目
     */
    @Select("<script>" +
            "SELECT * FROM questionbank WHERE question_id IN " +
            "<foreach item='id' collection='questionIds' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    List<QuestionBank> getQuestionsByIds(@Param("questionIds") List<Integer> questionIds);

    /**
     * 根据课程类型ID获取不同难度的题目数量分布
     */
    @Select("SELECT difficulty, COUNT(*) as count FROM questionbank " +
            "WHERE courseTypeid = #{courseTypeid} GROUP BY difficulty")
    List<java.util.Map<String, Object>> getDifficultyDistribution(@Param("courseTypeid") Integer courseTypeid);

    /**
     * 根据题目类型获取题目
     */
    @Select("SELECT * FROM questionbank WHERE courseTypeid = #{courseTypeid} " +
            "AND question_type = #{questionType} ORDER BY RAND() LIMIT #{limit}")
    List<QuestionBank> getQuestionsByType(@Param("courseTypeid") Integer courseTypeid,
                                          @Param("questionType") Integer questionType,
                                          @Param("limit") Integer limit);
} 